package org.tarena.dang.action.main;

import java.util.List;

import org.tarena.dang.action.BaseAction;
import org.tarena.dang.entity.Book;
import org.tarena.dang.entity.Category;
import org.tarena.dang.service.BookService;
import org.tarena.dang.service.ProductService;
import org.tarena.dang.util.DangFactory;

/**
 * 受理图书列表查询页面的业务请求
 * 
 * @author JiangZhongHua
 * @date 2013/02/17
 */

@SuppressWarnings("serial")
public class BookListAction extends BaseAction {

    //input 
	private int    pid;        //当前选中产品类别父节点ID
	private int    cid;        //产品类别ID
	private String parentName; //当前选中产品类别父节点的类别名称
	private int    pages;      //页数
	private int    pageSize;   //每页显示的数量
	
	//output
	private int    totalNum;   //当前父类下所有子类所包含的产品总数量
	private List<Category> cates;  //当前页面总分类的产品类别列表
	private List<Book> books;  //当前选中的产品类别分页显示的图书信息
	
	public String execute() throws Exception{
		ProductService service = DangFactory.getProductService();
		BookService bookService = DangFactory.getBookService();
		
		//查询图书列表页面分类导航菜单
		cates = service.getCategoryRoot(pid);
		
		//计算当前总类别的图书总数
		for (Category cate : cates) {
			totalNum+=cate.getProductSum();
		}
		
		//根据分页信息查询图书数据
		books = bookService.findBookPageByCatID(cid, pages, pageSize);
		return SUCCESS;
	}
	
	public int processPageSum(int sum,int size){
		int pageSum = sum/size;
		return (sum%size==0?pageSum:pageSum+1);
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public List<Category> getCates() {
		return cates;
	}

	public void setCates(List<Category> cates) {
		this.cates = cates;
	}

	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public int getPages() {
		return pages;
	}


	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}
